﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        private System.Threading.Thread thread;
        bool CommandWaiting = false;

        public Form1()
        {
            InitializeComponent();
            serialPort1.Open();
            thread = new System.Threading.Thread(new System.Threading.ThreadStart(this.GetImage));
            thread.Start();
        }

        private void GetImage()
        {
            byte[] buffer2 = new byte[10];
            int size;
            int i = 0;
            int b = 0;
            byte[] buffer = new byte[5000000];
            while (true)
            {
                if (CommandWaiting)
                {
                    serialPort1.Write(textBox1.Text);
                    System.Threading.Thread.Sleep(300);

                    UpdateTextBox(serialPort1.ReadExisting());
                    CommandWaiting = false;
                }
                else
                {
                    serialPort1.Write("I");
                    System.Threading.Thread.Sleep(200);
                    serialPort1.Read(buffer2, 0, 10);
                    if (buffer2[0] == '#' && buffer2[1] == '#')
                    {
                        size = ConvertFourBytesToScaledInt(buffer2[6], buffer2[7], buffer2[8], buffer2[9]);
                        if (size > 0 && size < 1000000)
                        {
                            b = serialPort1.Read(buffer, 0, size);
                            if (b != size)
                            {
                                Console.WriteLine("abc");
                            }
                            else
                            {
                                //code from http://www.ofitselfso.com/SRV1CSharpConsole.php
                                MemoryStream sTmp = new MemoryStream(buffer);
                                Bitmap bTemp = new Bitmap(sTmp);
                                pictureBox1.Image = bTemp;
                            }
                        }
                    }
                    else
                    {
                        i++;
                    }
                }
                serialPort1.ReadExisting();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            CommandWaiting = true;
        }

        //function from http://www.ofitselfso.com/SRV1CSharpConsole.php
        private int ConvertFourBytesToScaledInt(byte byte0, byte byte1, byte byte2, byte byte3)
        {
            int b1 = Convert.ToInt32(byte0) * (1);
            int b256 = Convert.ToInt32(byte1) * (256);
            int b256x256 = Convert.ToInt32(byte2) * (256 * 256);
            int b256x256x256 = Convert.ToInt32(byte3) * (256 * 256 * 256);
            return b1 + b256 + b256x256 + b256x256x256;
        }

        delegate void OutputUpdateDelegate(string data);
        public void UpdateTextBox(string data)
        {
            if ((data != null) && (data != ""))
            {
                if (listBox1.InvokeRequired)
                    listBox1.Invoke(new OutputUpdateDelegate(OutputUpdateCallback),
                    new object[] { data });
                else
                    OutputUpdateCallback(data); //call directly
            }
        }

        private void OutputUpdateCallback(string data)
        {
            listBox1.Items.Add(data);
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            thread.Abort();
            serialPort1.Close();
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            
        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            string temp = e.X.ToString();
            temp = temp.PadLeft(4, '0');
            string temp2 = e.Y.ToString();
            temp2 = temp2.PadLeft(4, '0');
            textBox1.Text = string.Format("v" + temp + temp2);
            CommandWaiting = true;
        }
    }
}
